package openapi.util;

import java.util.Map;
import java.util.TreeMap;

import org.apache.commons.codec.digest.DigestUtils;

import openapi.dto.ApiCommonParamDto;

public class SignUtil {

	public static final String MDF_CHARSET_UTF_8 = "UTF-8";

	public static String md5_sign(ApiCommonParamDto commonParam, String key) {
		return md5_sign(commonParam, key, "UTF-8");
	}

	@SuppressWarnings("rawtypes")
	public static String createSignStr(ApiCommonParamDto commonParam, String key) {

		Map<String, String> paramMap = new TreeMap<String, String>();
		paramMap.put("App_Sub_ID", commonParam.getAppSubId());
		paramMap.put("App_Token", commonParam.getAppToken());
		paramMap.put("Api_ID", commonParam.getApiID());
		paramMap.put("Api_Version", commonParam.getApiVersion());
		paramMap.put("Time_Stamp", commonParam.getTimeStamp());
		paramMap.put("Sign_Method", commonParam.getSignMethod());
		paramMap.put("Format", commonParam.getFormat());
		paramMap.put("Partner_ID", commonParam.getPartnerID());
		paramMap.put("Sys_ID", commonParam.getSysID());
		paramMap.put("App_Pub_ID", commonParam.getAppPubID());
		paramMap.put("REQUEST_DATA", commonParam.getReqdate());

		StringBuffer bufferSign = new StringBuffer();
		for (Map.Entry entry : paramMap.entrySet()) {
			bufferSign.append((String) entry.getKey()).append("=").append((String) entry.getValue()).append("&");
		}
		bufferSign.append(key);
		System.out.println("---------------------------------sign String：  " + bufferSign.toString());
		System.out.println();

		return bufferSign.toString();
	}

	public static String md5_sign(ApiCommonParamDto commonParam, String key, String input_charset) {
		String bufferSign = createSignStr(commonParam, key);
		String result = null;
		try {
			result = DigestUtils.md5Hex(bufferSign.toString().getBytes(input_charset)).toUpperCase();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	public static String md5_sign(String singStr) {
		String result = null;
		try {
			result = DigestUtils.md5Hex(singStr.getBytes("UTF-8")).toUpperCase();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	public static String rsa_encryptByPublicKey(String singStr, String publicKey) {
		String result = null;
		try {
			byte[] result_tem = RSAUtils.encryptByPublicKey(singStr.getBytes(), publicKey);
			result = Hex2Str.bytesToHexString(result_tem);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return result;
	}

	public static String rsa_decryptByPrivateKey(String singStr, String privateKey) {
		String result = null;
		try {
			byte[] result_tem = RSAUtils.decryptByPrivateKey(Hex2Str.hexStringToBytes(singStr), privateKey);
			result = new String(result_tem);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	public static String rsa_encryptByPrivateKey(String singStr, String privateKey) {
		String result = null;
		try {
			byte[] result_tem = RSAUtils.encryptByPrivateKey(singStr.getBytes(), privateKey);
			result = Hex2Str.bytesToHexString(result_tem);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return result;
	}

	public static String rsa_decryptByPublicKey(String singStr, String publicKey) {
		String result = null;
		try {
			byte[] result_tem = RSAUtils.decryptByPublicKey(Hex2Str.hexStringToBytes(singStr), publicKey);
			result = new String(result_tem);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

}
